// pages/mydetails/mydetails.js
const app = getApp()
var util = require('../../utils/util.js');
var server = require('../../utils/server');
const baseUrls = 'http://soundlab.jschunk.com:8085';
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    userInfo: [],
    user: [],
    tx: app.globalData.tupian + 'xxtx1.png',
    img: app.globalData.tupian + '13.png',
    img1: '',
    array: ['男', '女', ],
    index: 0,
    date: '2016-09-01',
    data: '2016-09-01',
    region: '中国',
    qm: '还没有签名，赶快来补充吧',
    tel:'未绑定',
    // customItem: '全部'
    hiddenmodalput: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    that = this
    var date = util.formatData(new Date());
    console.log(date)
    const userInfo = wx.getStorageSync('userInfo')
    that.setData({
      date: date,
      userInfo: userInfo,
      data:date
    })
    server.requestForm(false, 'get', 'user', {
        filter: {
          _id: app.globalData.userID
        }
      },
      function(e) {
        if(e.code == 0){
          var str = e.data[0].birthday
          var d = new Date(str.replace(/-/g, '/'));
     
          var times = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
          // var d = new Date(e.data[0].birthday);
          // var times = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
          console.log(e, times)
          that.setData({
            img: e.data[0].backgroundPicPath,
            img1: e.data[0].backgroundPicPath,
            region: e.data[0].city,
            date: times,
            qm: e.data[0].context,
            tel: e.data[0].tel
          })
          if (!e.data[0].birthday) {
            let date = util.formatData(new Date())
            that.setData({
              date: date
            })
          }
          if (!e.data[0].context) {
            let qm = '还没有签名，赶快来补充吧'
            that.setData({
              qm: qm
            })
          }
          if (!e.data[0].city) {
            let region = '中国'
            that.setData({
              region: region
            })
          }
          if (!e.data[0].tel) {
            let tel = '未绑定'
            that.setData({
              tel: tel
            })
          }
          if (!e.data[0].backgroundPicPath) {
            let img = app.globalData.tupian + '13.png'
            that.setData({
              img: img
            })
          }
        }else{
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 1000,
          });
        }

      })
  },

  phone: function(e) {
    wx.navigateTo({
      url: '../phone/phone',
    })
  },

  //更换图片
  image: function(e) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        that.shangchuan(tempFilePaths[0])
        that.setData({
          img: tempFilePaths[0],
        })
      }
    })
  },
  // 性别选择器
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  // 日期选择器
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  //城市选择器
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value[2])
    this.setData({
      region:e.detail.value[1] + '-'+ e.detail.value[2]
    })
  },

  // 修改签名
  nc: function(e) {
    var nickName = e.detail.value
    if (nickName == '') {
      this.setData({
        qm: '还没有签名，赶快来补充吧',
      })
    } else {
      this.setData({
        qm: nickName
      })
    }
  },
  nicheng: function() {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  xiugai: function(e) {
    console.log(that.data, that.data.img)

    setTimeout(function() {
      if (that.data.img1 == '') {
        var data = {
          _id: app.globalData.userID,
          birthday: that.data.date,
          city: that.data.region,
          context: that.data.qm
        }
      }else{
        var data = {
          _id: app.globalData.userID,
          backgroundPicPath: that.data.img1,
          birthday: that.data.date,
          city: that.data.region,
          context: that.data.qm
        }
      }
      console.log('data', data)
      console.log('baseUrls + that.data.img1', baseUrls + that.data.img1)
      server.requestForm(false, 'put', 'user',data,
        function(e) {
          console.log(e)
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 1000,
          });
          setTimeout(function () {
            that.onLoad()
          }, 500)

        })
    }, 500)

  },

  shangchuan: function(sscc) {
    server.uploadFile(sscc, function(e) {
      console.log('上传成功', e)
      var obj = JSON.parse(e.data); // --&gt; parse error 
      console.log('obj', obj)
      that.setData({
        img1: obj.data.url
      })
    })
  },

  //取消按钮  
  cancel: function() {
    this.setData({
      hiddenmodalput: true,
      qm: '还没有签名，赶快来补充吧',
    });
  },
  //确认  
  confirm: function(e) {
    console.log('qr')
    this.setData({
      hiddenmodalput: true
    });
  }
})